#include <func.h>
#include <pthread.h>

long long sum1(long long arg)
{
    int a = 0;
    for(int i = 1; i <= arg; ++i)
        a += i;
    return a;
}

void* start_routine1(void* arg)
{
    return (void*)sum1((long long)arg);
}


long long sum2(long long arg)
{
    int a = 0;
    for(int i = (arg / 2) + 1; i <= arg; ++i)
        a += i;
    return a;
}

void* start_routine2(void* arg)
{
    return (void*)sum2((long long)arg);
}


int main(void)
{
    long long n;
    printf("Please input a long long int: ");
    scanf("%lld", &n);

    pthread_t thread_1;
    int err = pthread_create(&thread_1, NULL, start_routine1, (void*)(n / 2));
    if(err)
        error(1, err, "pthread_create");

    pthread_t thread_2;
    pthread_create(&thread_2, NULL, start_routine2, (void*)n);
    if(err)
        error(1, err, "pthread_create");

    long long sum1 = 0;
    pthread_join(thread_1, (void**)&sum1);
    if(err)
        error(1, err, "pthread_create");

    long long sum2 = 0;
    pthread_join(thread_2, (void**)&sum2);
    if(err)
        error(1, err, "pthread_create");

    printf("sum1 + sum2 = %lld\n", sum1 + sum2);
    return 0;
}

